package com.ruoyi.app.controller

import com.github.binarywang.wxpay.exception.WxPayException
import com.ruoyi.app.common.Data
import com.ruoyi.app.service.PayApiService
import com.ruoyi.back.domain.dto.UserMemberOrderDto
import com.ruoyi.back.enums.PayTypeEnum
import com.ruoyi.back.service.UserMemberOrderService
import io.swagger.annotations.Api
import io.swagger.annotations.ApiOperation
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.RequestBody
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController
import javax.servlet.http.HttpServletRequest

@RestController
@RequestMapping("api/pay")
@Api(tags = ["支付api"])
class PayApiController {

    @Autowired
    private lateinit var payApiService: PayApiService

    @Autowired
    private lateinit var userMemberOrderService: UserMemberOrderService

    @ApiOperation(value = "微信预支付")
    @PostMapping("createOrder")
    @Throws(WxPayException::class)
    fun <T> createOrder(userMemberOrderDto: UserMemberOrderDto): Data<T> = payApiService.createWxOrder(userMemberOrderDto)

    @ApiOperation("创建阿里支付")
    @PostMapping("createAliOrder")
    fun createAliOrder(userMemberOrderDto: UserMemberOrderDto): Data<String> = payApiService.createAliOrder(userMemberOrderDto)

    @ApiOperation(value = "微信支付回调", hidden = true)
    @PostMapping("wxNotify")
    fun wxNotify(@RequestBody xmlData: String): String = payApiService.wxNotify(xmlData)


    @ApiOperation(value = "微信支付回调1", hidden = false)
    @PostMapping("wxNotify1")
    fun updateMemberPayStatus(orderNum: String) = userMemberOrderService.updateMemberPayStatus(orderNum, PayTypeEnum.WEI_XIN)

    @ApiOperation(value = "阿里支付回调", hidden = true)
    @PostMapping("aliNotify")
    fun aliNotify(request: HttpServletRequest): String = payApiService.aliNotify(request)
}